package com.epu.itepu.models;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.media.ExifInterface;

public class PhotoProperties {
	private String name;
	private String location;
	private String type;
	private Date timeStamp;
	private int width;
	private int heigh;
	private Double latitude;
	private Double longitude;

	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

	public PhotoProperties() {
		this.name = null;
		this.location = null;
		this.type = null;
		this.timeStamp = null;
		this.width = 0;
		this.heigh = 0;
		this.latitude = null;
		this.longitude = null;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Date getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(Date timeStamp) {
		this.timeStamp = timeStamp;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeigh() {
		return heigh;
	}

	public void setHeigh(int heigh) {
		this.heigh = heigh;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}

	public PhotoProperties(ExifInterface exif, String path)
			throws ParseException {
		this.name = path.substring(path.lastIndexOf("/") + 1);
		this.location = path.substring(0, path.lastIndexOf("/"));
		String temp = exif.getAttribute(ExifInterface.TAG_DATETIME);
		if (temp != null) {
			this.timeStamp = dateFormat.parse(temp);
		}
		temp = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
		if (temp != null) {
			this.width = Integer.parseInt(temp);
		}
		temp = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
		if (temp != null) {
			this.heigh = Integer.parseInt(temp);
		}
		
		temp = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
		String temp2 = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
		if (temp != null && temp2 != null) {
			this.latitude = (double) convertRationalLatLonToFloat(temp, temp2);
		}
		
		temp = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
		temp2 = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
		if (temp != null && temp2 != null) {
			this.longitude = (double) convertRationalLatLonToFloat(temp, temp2);
		}
	}

	private static float convertRationalLatLonToFloat(String rationalString,
			String ref) {
		try {
			String[] parts = rationalString.split(",");

			String[] pair;
			pair = parts[0].split("/");
			int degrees = (int) (Float.parseFloat(pair[0].trim()) / Float
					.parseFloat(pair[1].trim()));

			pair = parts[1].split("/");
			int minutes = (int) ((Float.parseFloat(pair[0].trim()) / Float
					.parseFloat(pair[1].trim())));

			pair = parts[2].split("/");
			double seconds = Double.parseDouble(pair[0].trim())
					/ Double.parseDouble(pair[1].trim());

			double result = degrees + (minutes / 60.0) + (seconds / 3600.0);
			if ((ref.equals("S") || ref.equals("W"))) {
				return (float) -result;
			}
			return (float) result;
		} catch (RuntimeException ex) {
			// if for whatever reason we can't parse the lat long then return
			// null
			return 0f;
		}
	}

}
